<?php

namespace app\push\controller;
use think\worker\Server;
use Channel\Client;

class Publish extends Server
{
    protected $socket = 'http://0.0.0.0:10000';
    protected $option = ['name'=>'publish'];
    public function onWorkerStart($worker){
        //连接通道
        Client::connect('127.0.0.1',12138);
    }
    public function onMessage($connection,$data){
        $connection->send('ooook');
        //http://47.100.13.53:10000/?content=login success&connection_id=2&worker_id=0   单个用户发布
        //http://47.100.13.53:10000/?content=login success  //所有用户
        if(!isset($_GET['content'])){
            return;
        }
        if(isset($_GET['worker_id'])&&isset($_GET['connection_id'])){
            $data = ['connection_id'=>$_GET['connection_id'],'content'=>$_GET['content']];
            Client::publish($_GET['worker_id'],$data);
        }else{

            Client::publish('broadcast',['content'=>$_GET['content']]);
        }
    }
}